package ${packageName};

import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.*;

<#if imports??>
<#list imports as import>
import ${import};
</#list>
</#if>

/**
 * ${className} JSON转换器
 * 自动生成的代码
 */
public class ${className}JsonConverter {
    private static final ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 将JSON字符串转换为对象
     */
    public static ${className} fromJson(String json) {
        try {
            return objectMapper.readValue(json, ${className}.class);
        } catch (Exception e) {
            throw new RuntimeException("JSON转换失败", e);
        }
    }

    /**
     * 将对象转换为JSON字符串
     */
    public static String toJson(${className} obj) {
        try {
            return objectMapper.writeValueAsString(obj);
        } catch (Exception e) {
            throw new RuntimeException("JSON转换失败", e);
        }
    }

    /**
     * 将JSON字符串转换为对象列表
     */
    public static List<${className}> fromJsonList(String json) {
        try {
            return objectMapper.readValue(json,
                objectMapper.getTypeFactory().constructCollectionType(List.class, ${className}.class));
        } catch (Exception e) {
            throw new RuntimeException("JSON转换失败", e);
        }
    }

    /**
     * 将对象列表转换为JSON字符串
     */
    public static String toJsonList(List<${className}> list) {
        try {
            return objectMapper.writeValueAsString(list);
        } catch (Exception e) {
            throw new RuntimeException("JSON转换失败", e);
        }
    }
}
